package com.niodata.dp.monitor.components.task.dao;

import com.niodata.dp.monitor.sharelib.entity.task.TaskExecution;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * Created by ndp on 18-2-5.
 */

@Mapper
public interface TaskExecutionMapper {

  public void insert(@Param("execList") List<TaskExecution> execList);


  @ResultMap("taskExecutionResultMap")
  public List<TaskExecution> getTaskExecutionByOwner(@Param("taskId") String taskId,
        @Param("userId") long ownerId,
        @Param("ids") List<Long> ids, @Param("offset") int offset,
        @Param("count") int count, @Param("status") String status,
        @Param("startTime") String startTime, @Param("endTime") String endTime);

  @ResultMap("taskExecutionResultMap")
  public List<TaskExecution> getAllTaskExecutionByOwner(@Param("taskId") String taskId,
        @Param("userId") long ownerId,
        @Param("ids") List<Long> ids, @Param("offset") int offset,
        @Param("count") int count,
        @Param("startTime") String startTime, @Param("endTime") String endTime);


  @Select("select * from TASK_EXECUTION "
        + "where STATUS='RUNNING' OR STATUS='PREPARING'")
  @ResultMap("taskExecutionResultMap")
  public List<TaskExecution> getRunningTaskExecution();

  @Select("select * from TASK_EXECUTION "
        + "where START_TIME >= #{startTime} and START_TIME <= #{stopTime}")
  @ResultMap("taskExecutionResultMap")
  public List<TaskExecution> getTaskExecutionByTime(@Param("startTime") String startTime,
        @Param("stopTime") String stopTime);

  @Update("update TASK_EXECUTION set STATUS=#{status},START_TIME=#{startTime},"
        + "END_TIME=#{endTime} where EXEC_ID=#{execId}")
  public void update(@Param("execId") int execId, @Param("status") String status,
        @Param("startTime") Date startTime,
        @Param("endTime") Date endTime);

  @Update("update TASK_EXECUTION set STATUS=#{status},START_TIME=#{startTime},"
    + "END_TIME=#{endTime},EXECUTOR_ID=#{executorId} where EXEC_ID=#{execId}")
  public void updateWithExecutorId(@Param("execId") int execId, @Param("executorId") int executorId,
                     @Param("status") String status,
                     @Param("startTime") Date startTime,
                     @Param("endTime") Date endTime);

  @Select("select * from TASK_EXECUTION WHERE EXEC_ID=#{execId}")
  @ResultMap("taskExecutionResultMap")
  public TaskExecution getTaskExecution(@Param("execId") int execId);

  @Select("select * from TASK_EXECUTION order by EXEC_ID desc limit 10")
  @ResultMap("taskExecutionResultMap")
  public List<TaskExecution> getRecentlyExecutions();

  public List<TaskExecution> getRecentlyFinishedExecsByUser(@Param("userId") long userId,
        @Param("groupIds") List<Long> groupIds, @Param("minEndTime") Date minEndTime);

}
